Page({
  data: {
    note: {
      title: "",
      content: "",
      tag: "",
      tagText: "",
      mood: "",
      moodText: "",
      weather: "",
      weatherText: "",
      location: "",
      time: ""
    },
    isEdit: false,
    noteId: null
  },

  onLoad(options) {
    if (options.id) {
      this.setData({
        isEdit: true,
        noteId: options.id
      });
      this.loadNoteData(options.id);
    }
  },

  loadNoteData(id) {
    const pages = getCurrentPages();
    const indexPage = pages.find(page => page.route === 'pages/index/index');
    
    if (indexPage && indexPage.data.notes) {
      const note = indexPage.data.notes.find(item => item.id == id);
      if (note) {
        this.setData({ note: { ...note } });
      }
    }
  },

  onTitleInput(e) {
    const note = this.data.note;
    note.title = e.detail.value;
    this.setData({ note });
  },

  onContentInput(e) {
    const note = this.data.note;
    note.content = e.detail.value;
    this.setData({ note });
  },

  selectTag(e) {
    const note = this.data.note;
    note.tag = e.currentTarget.dataset.tag;
    note.tagText = e.currentTarget.dataset.text;
    this.setData({ note });
  },

  selectMood(e) {
    const note = this.data.note;
    note.mood = e.currentTarget.dataset.mood;
    note.moodText = e.currentTarget.dataset.text;
    this.setData({ note });
  },

  selectWeather(e) {
    const note = this.data.note;
    note.weather = e.currentTarget.dataset.weather;
    note.weatherText = e.currentTarget.dataset.text;
    this.setData({ note });
  },

  onLocationInput(e) {
    const note = this.data.note;
    note.location = e.detail.value;
    this.setData({ note });
  },

  onCancel() {
    wx.navigateBack();
  },

  onSave() {
    const note = this.data.note;
    
    if (!note.title.trim() || !note.content.trim()) {
      wx.showToast({
        title: '标题和内容不能为空',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    
    // 处理时间逻辑
    const now = new Date();
    const hours = now.getHours().toString().padStart(2, '0');
    const minutes = now.getMinutes().toString().padStart(2, '0');
    
    let timeText = '';
    const today = new Date();
    today.setHours(0, 0, 0, 0);
    
    if (now - today < 86400000) {
      timeText = `今天 ${hours}:${minutes}`;
    } else if (now - today < 2 * 86400000) {
      timeText = `昨天 ${hours}:${minutes}`;
    } else {
      const month = (now.getMonth() + 1).toString().padStart(2, '0');
      const day = now.getDate().toString().padStart(2, '0');
      timeText = `${month}-${day} ${hours}:${minutes}`;
    }
    note.time = timeText;
    
    // 关键修改：用concat替代扩展运算符，避免Babel辅助函数
    const pages = getCurrentPages();
    const indexPage = pages.find(page => page.route === 'pages/index/index');
    
    if (indexPage) {
      // 原代码：let notes = [...(indexPage.data.notes || [])];
      // 修改为兼容性写法：
      let notes = (indexPage.data.notes || []).concat();
      
      if (this.data.isEdit) {
        const index = notes.findIndex(item => item.id == this.data.noteId);
        if (index !== -1) notes[index] = note;
      } else {
        note.id = Date.now();
        // 原代码：notes.unshift(note);
        // 改用concat创建新数组（避免修改原数组引用）
        notes = [note].concat(notes);
      }
      
      indexPage.setData({ notes, filteredNotes: notes });
      wx.setStorageSync('notes', notes);
      
      wx.showToast({
        title: '保存成功',
        icon: 'success',
        duration: 1500,
        success: () => {
          setTimeout(() => wx.navigateBack(), 1500);
        }
      });
    }
  }
});
